const { style, modpath, themepath } = require("../../config.json");
const { getFileNames } = require("../utils/generator");
const root = require("../../root");
const config = require("../../config.json");
const fs = require("fs");
const path = require("path");
const prog = require("commander");
const { success, error, bar, welcome } = require("../utils/tools");

function handleOptions(keys, callback) {
	let options = prog.program.opts();
	let values = [];
	if (typeof keys === "string") {
		// 选项是否选中,没选中return
		if (!Object.keys(options).includes(keys)) return;
		values = options[keys];
	} else if (keys instanceof Array) {
		for (let key of keys) {
			// 选项是否选中,没选中处理下一个选项
			if (!Object.keys(options).includes(keys)) continue;
			values.push(options[key]);
		}
		//一个都没选中直接rerun
		if (values === []) {
			return;
		}
	}

	return callback(values);
}
function set(config, key, data) {
	return new Promise((resolve, reject) => {
		config[key] = data;
		try {
			let data = fs.writeFileSync(
				path.resolve(root, "config.json"),
				JSON.stringify(config)
			);
			resolve(data);
		} catch (e) {
			reject(e);
		}
	});
}

function turnAction() {
	let o = true;
	// 处理 options
	handleOptions("dest", async (dest) => {
		await set(config, "outpath", dest)
			.then((res) => {
				success(`dest => ${dest} 参数处理成功!`);
			})
			.catch((e) => {
				error(`source => ${source} 参数处理失败!`);
			});
	});

	handleOptions("source", (source) => {
		set(config, "sourcepath", source)
			.then((res) => {
				success(`source => ${source} 参数处理成功!`);
				// 确保在参数处理后回调
				startMain();
				o = false;
			})
			.catch((e) => {
				error(`source => ${source} 参数处理失败!`);
			});
	});
	if (o) {
		startMain();
	}
	function startMain() {
		// 读取 sourcepath 下所有的 md 文件
		const { sourcepath } = require("../../config.json");
		success(`* 感谢你使用 fast-md!🌈 🌈 🌈 `);
		success(`* 正在处理${sourcepath}📄....\n`);
		bar.start(100, 0);
		const mds = getFileNames(sourcepath, false, false);
		for (let md of mds) {
			bar.increment(100 / mds.length);

			if (typeof md === "string" && md.startsWith(".")) {
				continue;
			}

			const writebody = require("./body");
			writebody(sourcepath, `${md}.md`, `${md}.html`).then((res) => {
				// 写入样式
				const wireStyle = require("./style");
				wireStyle(style, modpath, themepath, `${md}.html`);
			});
		}
		bar.stop();
		welcome(`* 处理成功,共计 ${mds.length} 个文件. ( 多的是隐藏文件 )`);
	}
}

function showConfigAction() {
	console.log("-------------------------------------");
	console.log("配置信息:");
	for (let key of Object.keys(config)) {
		console.log(`  ${key} : ${config[key]}`);
	}
	console.log("-------------------------------------");
}
function setModAction(modpath) {
	set(config, "modpath", modpath)
		.then((res) => {
			console.log(`modpath => ${modpath} 设置成功!`);
		})
		.catch((e) => console.log(`modpath => ${modpath} 设置成功!`));
}
function setThemeAction(themepath) {
	set(config, "themepath", themepath)
		.then((res) => {
			console.log(`themepath => ${themepath} 设置成功!`);
		})
		.catch((e) => console.log(`themepath => ${themepath} 设置失败!`));
}
module.exports = {
	turnAction,
	showConfigAction,
	setModAction,
	setThemeAction,
};
